package com.maigu.controller;

import com.maigu.VO.ProductInfoVO;
import com.maigu.VO.ProductVO;
import com.maigu.VO.ResultVO;
import com.maigu.dataobject.ProductCategory;
import com.maigu.dataobject.ProductInfo;
import com.maigu.service.CategoryService;
import com.maigu.service.ProductService;
import com.maigu.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResultVO List()
 {

     //查询所有上架商品
     List<ProductInfo> productInfoList = productService.findUpAll();
     //查询所以类别
//     List<Integer>categoryTypeList=new ArrayList<>();
//     for(ProductInfo productInfo1:productInfoList)
//     {
//         categoryTypeList.add(productInfo1.getCategoryType());
//     }
     List<Integer>categoryTypeList=productInfoList.stream().map(e->e.getCategoryType()).collect(Collectors.toList());
     List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

     List<ProductVO>productVOList=new ArrayList<>();
     for (ProductCategory productCategory:productCategoryList)
     {
         ProductVO productVO=new ProductVO();
         productVO.setCategoryName(productCategory.getCategoryName());
         productVO.setCategoryType(productCategory.getCategoryType());

         List<ProductInfoVO> productInfoVOList=new ArrayList<>();
         for(ProductInfo productInfo:productInfoList)
         {
             if(productInfo.getCategoryType().equals(productCategory.getCategoryType()))
             {
                 ProductInfoVO productInfoVO = new ProductInfoVO();
//                 productInfoVO.setProductId(productInfo.getProductId());
//                 productInfoVO.setProductName(productInfo.getProductName());
//                 productInfoVO.setProductPrice(productInfo.getProductPrice());
//                 productInfoVO.setProductDescription(productInfo.getProductDescription());
//                 productInfoVO.setProductIcon(productInfo.getProductIcon());
                 BeanUtils.copyProperties(productInfo,productInfoVO);
                 productInfoVOList.add(productInfoVO);
             }

         }

         productVO.setProductInfoVOList(productInfoVOList);
         productVOList.add(productVO);
     }
     return ResultVOUtil.success(productVOList);

}
}
